package com.zlm.controller;

import com.zlm.vo.UserRegistVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 郑
 */
@Controller
public class LoginController {

    @PostMapping("regist")
    public String regist(@Valid UserRegistVo userRegistVo, BindingResult bindingResult, Model model){
        if (bindingResult.hasErrors()) {
            HashMap<String, String> map = new HashMap<>();
            /**
             * .map( error -> {
             *                 String field = error.getField();
             *                 String errorMsg = error.getDefaultMessage();
             *                 return map.put(field,errorMsg);
             *             })
             */
            Map<String, String> collect = bindingResult.getFieldErrors()
                    .stream().collect(Collectors.toMap(FieldError::getField, error -> error.getDefaultMessage()));
            model.addAttribute("msg",collect);
            //校验出错跳转注册页
            return "reg";
        }
        //注册


        //注册成功回到首页或登录页
        return "redirect:/login.html";

    }
}
